﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using DotNet.Business;
using DotNet.Model;
using DotNet.Common;
using System.Threading;
using System.Diagnostics;
using System.IO;
public partial class Admin_addProduct : basePage
{ 
    productClassBll pcBll = new productClassBll();
    productBll pBll = new productBll();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
            ViewState["dt"] = pcBll.GetDataTable();
            if (Request.QueryString["state"] != null)
            {
                string state = Request.QueryString["state"].ToString();
                switch (state.Trim())
                {
                    case "insert":
                        Insert();
                        break;
                    case "load":
                        upload();
                        break;
                    case "create":
                        CreateDir();
                        break; 
                    case "canel":
                        canel();
                        break;
                    default:
                        // instance = accInstance;
                        break;
                }
            }
           
           
        }

    }
    #region 重新生成文件夹
    public void canel() 
    {
        string path = Server.MapPath("~") + "/UploadFiles/productUpload/" + Request.QueryString["dirName"].ToString();        
        DirFile.DeleteDir("UploadFiles\\productUpload\\" + Request.QueryString["dirName"].ToString(),true);
        Response.Write("1");
        Response.End();
    }
    #endregion

    public void upload()
    {

    }
    #region 创建文件夹
    public void CreateDir() 
    {
        string dirName = Request.QueryString["dirName"].Trim();
        string path = Server.MapPath(@"~\UploadFiles\productUpload\"+dirName);
        if (!DirFile.CheckDir(path))
        {
            //Directory.CreateDirectory(path + @"\" + dirName);
            DirFile.CreateDir("UploadFiles\\productUpload\\"+dirName);
            Session["FCKeditor:UserFilesPath"] = "~/UploadFiles/productUpload/" + dirName;
            Response.Write("1");
            Response.End();
        }
        else 
        {
            Response.Write("已存在该目录！");
            Response.End();
        }
        
    }  
    #endregion

    #region 检测是否有重复的型号---郑同飞
    public void checkProModel()
    {
        if (pBll.Exists(Request.Form["dirName"].ToString()))
        {
            Response.Write("产品型号已经存在！");
            Response.End();
        }       
    }
    #endregion


    #region 添加产品
    public void Insert()
    {


        checkProModel();
        product p = new product();
        p.isBast = false;
        p.isCan = false;
        p.isIndex = false;
        p.IsNew = false;
        p.pButtom = Request.Form["up"].ToString();
        p.pClassId = Convert.ToInt32(Request.Form["selectDp"].ToString());
        p.pDate = DateTime.Now;
        p.pDescription = Request.Form["des"].ToString();
        p.pInfo =Server.HtmlDecode(Request.Form["FCKeditor1"].ToString());
        p.pKey = Request.Form["key"].ToString();
        p.Price = 0;
        p.productName = Request["m_name"].ToString();
        p.pSort = Convert.ToInt32(Request.Form["psort"].ToString());
        p.ptag = Request.Form["tag"].ToString();
        p.pTitle = Request.Form["title"].ToString();
        p.Views = Convert.ToInt32(Request.Form["view"].ToString());
        p.eDate = DateTime.Now;
        p.Productmodel = Request.Form["dirName"].ToString().Trim();
        if (Request.Form["imgSrc"].ToString() != "")
            p.pSourse = Request.Form["imgSrc"].ToString();
        else
            p.pSourse = "";      
            if (pBll.Insert(p))
            {
                DotSession.Delete("ProductImg");
                Response.Write("1");                
            }
            else 
            {
                Response.Write("生成失败！");
            }  
        Response.End();
    }
    #endregion

    #region "抛弃服务器控件,自己绑定下拉框"
    public string ProductClassDp(string name)
    {
        StringBuilder str = new StringBuilder();
        str.Append("<select name='selectDp' id='" + name + "' style='width:192px;'>");
        str.Append("<option value='0'>---顶级分类---</option>");
        str.Append(AddItems(0, 0, new StringBuilder()));
        str.Append("</select>");
        return str.ToString();
    }

    public string AddItems(int ParentID, int length, StringBuilder sb)
    {

        DataView dvTree = pcBll.GetDataView(ViewState["dt"] as DataTable, ParentID);
        length++;
        foreach (DataRowView row in dvTree)
        {
            string str = addtab(length);
            sb.AppendFormat("<option value='" + row["pClassId"].ToString() + "'>{0}</option>", str + "|--" + row["pClassName"].ToString());
            AddItems(Convert.ToInt32(row["pClassId"].ToString()), length, sb);
        }
        return sb.ToString();
    }
    private string addtab(int i)
    {
        string mark = "&nbsp;";
        if (i != 1)
        {
            for (int j = 0; j < i - 1; j++)
            {
                mark += "&nbsp;&nbsp;&nbsp;&nbsp;";
            }
        }
        return mark;
    }
    #endregion
}
